package com.javajun.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.javajun.entity.Manager;
import com.javajun.service.ManagerService;

/**
 * @ClassName: ManagerController
 * @Description: 班主任的控制器
 * @author: xlm
 * @date: 2018年10月15日
 */
@Controller
@RequestMapping(value="/admin/manager")
public class ManagerController {

	@Autowired
	private ManagerService managerService;
	
	// 显示所有老师
	@RequestMapping(value="/manage")
	public String manager(ModelMap map, Manager entity) {
		List<Manager> models = managerService.findAll(managerService.BuildSpec(entity));
		map.put("searchs", entity);
		map.put("models", models);
		return "manager/manage";
	}
	
	// 修改页面
	@RequestMapping(value="/edit", method=RequestMethod.GET)
	public String Edit(ModelMap map, Integer id) {
		Manager model = new Manager();
		if(id != null) {
			model = managerService.findById(id);
		}
		map.put("model", model);
		return "manager/edit";
	}
	
	// 修改之后的保存方法
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public String Save(ModelMap map, Manager entity) {
		Manager manager = new Manager();
		if(entity.getId() != null) {
			manager = managerService.findById(entity.getId());
		}
		if(entity.getTeaNum() != null) {
			manager.setTeaNum(entity.getTeaNum());
		}
		if(entity.getName() != null) {
			manager.setName(entity.getName());
		}
		if(entity.getSex() != null) {
			manager.setSex(entity.getSex());
		}
		if(entity.getPassword() != null) {
			manager.setPassword(entity.getPassword());
		}
		
		managerService.save(manager);
		return "redirect:manage";
	}
	
	// 删除一个班主任
	@RequestMapping(value="/delete")
	public String Delete(ModelMap map, Integer id) {
		if(id != null) {
			Manager entity = managerService.findById(id);
			managerService.delete(entity);
		}
		return "redirect:manage";
	}
	
	// 新增页面
	@RequestMapping(value="/add")
	public String Add(ModelMap map) {
		return "manager/add";
	}
}
